/**axios封装
 * 请求拦截、相应拦截、错误统一处理
 */
import axios from 'axios';
import QS from 'qs';
import {message} from 'ant-design-vue';
import store from '../store/index'
import router from '@/router'

message.config({
  maxCount: 1
});

// 环境的切换
axios.defaults.baseURL = 'http://47.107.231.209:11900'

// 请求超时时间
axios.defaults.timeout = 10000;

// post请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';

// 请求拦截器
axios.interceptors.request.use(
  config => {
    const token = store.state.token;
    token && (config.headers.Authorization = token);
    return config;
  },
  error => {
    return Promise.error(error);
  })
const key = 'old';
// 响应拦截器
axios.interceptors.response.use(
  response => {
    if (response.status === 200) {
      if (response.data.code === 200) {
        return Promise.resolve(response);
      } else {
        switch (response.data.code) {
          //管理员未登录
          case 4000:
            message.error({
              content: "管理员登录过期", onClose: () => {
                router.replace({
                  path: '/backstageLogin',
                })
              }
            })
            break;
          //用户未登录
          case 4002:
            message.error(response.data.msg)
            router.replace({
              path: '/login',
            });
            break;
          //当前用户没有访问权限
          case 4001:
            message.error({content: response.data.msg + "，请重新登录", key})
            router.replace({
              path: '/login',
            });
            break;
          case 4003:
            message.error(response.data.msg)
            message.warn("不是指定局域网")
            break;
          case 4004:
            message.error(response.data.msg)
            break;
          default:
            return Promise.resolve(response);
        }
      }
    } else {
      return Promise.reject(response);
    }
  },
  // 服务器状态码不是200的情况

  error => {
    console.log("Request failed with status code 500");
  }
);


export function Request(method, url, params) {
  if (method === "GET" || method === "get") {
    return new Promise((resolve, reject) => {
      axios.get(url, {
        params: params
      })
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err.data)
        })
    });
  } else if (method === "post" || method === "POST") {
    return new Promise((resolve, reject) => {
      axios.post(url, QS.stringify(params))
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err.data)
        })
    });
  } else if (method === "PUT" || method === "put") {
    return new Promise((resolve, reject) => {
      axios.put(url, QS.stringify(params))
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err.data)
        })
    });
  } else if (method === "DELETE" || method === "delete") {
    return new Promise((resolve, reject) => {
      axios.delete(url, {
        params: params
      })
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err.data)
        })
    });
  } else {
    return new Promise(message.error("无法发送请求"))
  }
}
